<?php
/*
合并家长信息，
*/
class MergeparentsController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;


	public function init() {
		$this->commonAction();	
	}
	
	//查询所有的家长信息
   public function indexAction() {
		try { 
		   $where='';	
		   $result=array();
		   $studentname =  trim($this->_request->getParam ( 'studentname' )); 
		   $mobile =  trim($this->_request->getParam ( 'mobile' )); 
			
			if($studentname){
				$studentname=str_replace('　','',str_replace(' ','',str_replace('，',',',$studentname)));
				if(strpos($studentname, ',')>0){
					$studentname=explode(',',$studentname);
					 $this->view->studentname = implode(',',$studentname);
				}else{
					$this->view->studentname = $studentname;
				}
				
				if(is_array($studentname)){
					$where .=' and (';
					foreach($studentname as $key => $value){						
						if($key!=0)
							$where .=' or cnname like "%'.$value.'%" or realname like "%'.$value.'%"';
						else
							$where .=' cnname like "%'.$value.'%" or realname like "%'.$value.'%"';
					}
					$where .=' )';
				}else{					
					$where .=' and ( cnname like "%'.$studentname.'%"  or realname like "%'.$studentname.'%")';
				}					
			}
			
			if($mobile){
				$where.=' and mobile ="'.$mobile.'"';
				$this->view->mobile = $mobile;
			}
			
			if($studentname || $mobile){		
				$sql="select uid,pid,cnname,nikename,sex,birthday,realname,mobile,username,ischeck_mobile,isinstalled,groupname,schoolname,classname from group_student_class_parents_v where ismainid=1 and inactive=0  ".$where; 
				$studentlist = $this->_dbAdapter->fetchAll ($sql);
				
				if($studentlist){
					foreach($studentlist as $key => $value){
						if(!isset($result[$value['pid']])){
							$result[$value['pid']]['parent']['realname']=stripslashes($value['realname']);
							$result[$value['pid']]['parent']['pid']=$value['pid'];
							$result[$value['pid']]['parent']['mobile']=$value['mobile'];
							$result[$value['pid']]['parent']['username']=stripslashes($value['username']);
							$result[$value['pid']]['parent']['ischeck_mobile']=$value['ischeck_mobile'];
							$result[$value['pid']]['parent']['isinstalled']=$value['isinstalled'];
						}
						if(!isset($result[$value['pid']]['student'][$value['uid']])){
							$result[$value['pid']]['student'][$value['uid']]=array(
								'uid'=>$value['uid'],
								'cnname'=>stripslashes($value['cnname']),
								'nikename'=>stripslashes($value['nikename']),
								'sex'=>$value['sex'],
								'sexDisp'=>$value['sex']==1?'男':'女',
								'birthday'=>$value['birthday'],
								'age'=>$this->getAge($value['birthday']),
							);
						}

						$result[$value['pid']]['student'][$value['uid']]['class'][]=array(
								'groupname'=>$value['groupname'],
								'schoolname'=>$value['schoolname'],
								'classname'=>$value['classname'],
						);
					}
					
				}
			}
		   
		   
		   $this->view->result=$result;	

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function indexdeleteAction() {
		try {
			$cnname =  trim($this->_request->getParam ( 'cnname' )); 
			$uid =  trim($this->_request->getParam ( 'uid' )); 
			$mobile =  trim($this->_request->getParam ( 'mobile' )); 
			//print_r($cnname);echo '<br />';print_r($uid);echo '<br />';print_r($mobile);echo '<br />';exit;
			
			if( !$cnname || !$uid || !$mobile )
				throw new Exception ( '参数错误' );
			
/*			//找到他的groupid
			$sql='select * from group_school_class_student_v where uid='.$uid;
			$result=$this->_dbAdapter->fetchRow($sql);
			//print_r($result);exit;
			
			$isok=0;

			if($result['uid_class']){
				$delete1=$this->_dbAdapter->delete('student_class',"uid_student=".$result['uid']." and uid_class=".$result['uid_class']);
			}
*/			
			$sql='select * from student_parents where ismainid=1 and studentid="'.$uid.'"';
			$result=$this->_dbAdapter->fetchRow($sql);

			$isok=0;
			if($result){
				$delete2=$this->_dbAdapter->delete('student_parents',' ismainid=1 and studentid='.$uid);
				$isok=1;
			}
			if($isok)
				$this->feedback ( '删除成功', '注意', '/'.$this->controller .'/index/studentname/'.$cnname.'/mobile/'.$mobile, 'warning' );
			else	
				throw new Exception ( '删除失败 ！' );

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
		
	//向上合并
	public function opAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			
			$sql='select pid from student_parents where studentid="'.$p['uid'].'" and ismainid=1';
			$pid=$this->_dbAdapter->fetchRow($sql);
			
			if(!$pid)
				throw new Exception ( '更新失败 ！' );
			
			
			$result=$this->_dbAdapter->update('student_parents',array('pid'=>$p['pid']),'studentid="'.$p['uid'].'" and ismainid=1');
			
			if($result)
				$this->feedback ( '更新成功', '注意', '/'.$this->controller .'/index/studentname/'.$oldinfo['cnname'], 'warning' );
			else	
				throw new Exception ( '更新失败 ！' );

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	//合班
   public function changeclassAction() {
		try { 
		   $where='';	
		   $result=array();
		   $studentname =  trim($this->_request->getParam ( 'studentname' )); 
			
			if($studentname){
				$this->view->studentname = $studentname;
				
				$where .=' and  (cnname like "%'.$studentname.'%"  )';					
				$sql="select uid,pid,cnname,nikename,sex,birthday,realname,mobile,username,ischeck_mobile,isinstalled,groupname,schoolname,classname,uid_class from group_student_class_parents_v where ismainid=1 and inactive=0  ".$where;
				//echo $sql;
				$studentlist = $this->_dbAdapter->fetchAll ($sql);
				//print_r($studentlist);exit;
				if($studentlist){
					foreach($studentlist as $key => $value){
						if(!isset($result[$value['pid']])){
							$result[$value['pid']]['parent']['realname']=stripslashes($value['realname']);
							$result[$value['pid']]['parent']['pid']=$value['pid'];
							$result[$value['pid']]['parent']['mobile']=$value['mobile'];
							$result[$value['pid']]['parent']['username']=stripslashes($value['username']);
							$result[$value['pid']]['parent']['ischeck_mobile']=$value['ischeck_mobile'];
							$result[$value['pid']]['parent']['isinstalled']=$value['isinstalled'];
						}
						if(!isset($result[$value['pid']]['student'][$value['uid']])){
							$result[$value['pid']]['student'][$value['uid']]=array(
								'uid'=>$value['uid'],
								'cnname'=>stripslashes($value['cnname']),
								'nikename'=>stripslashes($value['nikename']),
								'sex'=>$value['sex'],
								'sexDisp'=>$value['sex']==1?'男':'女',
								'birthday'=>$value['birthday'],
								'age'=>$this->getAge($value['birthday']),
							);
						}

						$result[$value['pid']]['student'][$value['uid']]['class'][]=array(
								'groupname'=>$value['groupname'],
								'schoolname'=>$value['schoolname'],
								'classname'=>$value['classname'],
								'classid'=>$value['uid_class'],
						);
					}
					
				}
			}		   
		   //print_r($result);exit;
		   $uid_class='';
		   foreach($studentlist as $key1=>$value1){
			   if(!$uid_class){
			   	   $uid_class.=$value1['uid_class'];
			   }else{
			   	   $uid_class.=','.$value1['uid_class'];
			   }
		   }
		   //print_r($num);exit;
		   $this->view->result=$result;	
		   $this->view->uid_class=$uid_class;	

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}


	//操作合班
   public function opchangeAction() {
		try { 
			 $cnname =  trim($this->_request->getParam ( 'cnname' )); 
			 $uid_class =  trim($this->_request->getParam ( 'uid_class' )); 
			 $student_uid =  trim($this->_request->getParam ( 'student_uid' )); 
			 
			 $this->view->cnname=$cnname;
			 $this->view->uid_class=$uid_class;
			 $this->view->student_uid=$student_uid;
		   
		     $groupid = ( int ) $this->_request->getParam ( 'groups' );
		     $schoolid = $this->_request->getParam ( 'school' ); 		
	         $classid  = $this->_request->getParam ( 'class' ); 
	         $cnname  = trim($this->_request->getParam ( 'cnname' )); 
		     
		     $this->view->groups=$groupid; 
		     $this->view->school_id=$schoolid;  
		     $this->view->classid=$classid; 
		 	 $student='';	
   		     $where='';	 
			 if($schoolid){ 
				  $schoolarray=explode('_',$schoolid);  
				  $schoolid=$schoolarray['0'];
			 } 

			 if($student_uid){
			    $where .=' and  a.uid<>'.$student_uid.' ';
			 }
			 if($cnname){
			    $where .=' and  a.cnname="'.$cnname.'" ';
			 }
			 
			 if($uid_class){
			     $where .=' and  c.uid  not in ('.$uid_class.')';
			 }
			 
			 if($schoolid){
			    $where .=' and  a.schoolid='.$schoolid.' '; 
				$this->view->schoolid= $schoolid; 
			 } 
			 if($classid){
			    $where .=' and  c.classid='.$classid.' ';
				$this->view->classid= $classid; 
			 }
			 $student=array(); 
			 if($groupid){ 
			 	//$sql="select dbname  from groups where groupid=".$groupid;
			 	//// echo $sql;  exit;
			 //	$dbname = $this->_dbAdapter->fetchOne ($sql);  
		        //连接运营库
		        //$ydb=$this->getDbLink($groupname_en=$groupid);
		        //$ydb->query( 'set names utf8');
				$sql1="SELECT a.studentid,a.uid,a.schoolid,c.classid,c.uid as class_uid,c.classname,a.studentno,a.cnname,b.schoolname FROM student a LEFT JOIN school b on a.schoolid=b.schoolid left join class c on b.schoolid=c.schoolid where 1 ".$where;
		        $student=$this->_dbAdapter->fetchAll($sql1);
				if(!$student){
					$student=0;
				}
				//echo $sql1;echo '<br />';print_r($student);exit;          
		     }
				$this->view->student= $student; 

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function opchangeclassAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			
			if(!$p['htmlid'])
				throw new Exception ( '请选择 ！' );
			if(!$p['groupid'])
				throw new Exception ( '参数错误 ！' );
			if(!$p['student_uid'])
				throw new Exception ( '参数错误 ！' );
			if(!$p['class_uid'])
				throw new Exception ( '参数错误 ！' );
			
			foreach($p['htmlid'] as $key=>$value){
			 	//$sql="select dbname  from groups where groupid=".$p['groupid'];
			 	// echo $sql;  exit;
			 	//$dbname = $this->_dbAdapter->fetchOne ($p['groupid']);  
		        //连接运营库
		        //$ydb=$this->getDbLink($groupname_en=$dbname);
		        //$ydb->query( 'set names utf8');
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////				
				
				$sql="select studentno from student where studentid=".$value." and class_uid=".$p['class_uid'][$key];
				$studentno=$this->_dbAdapter->fetchOne($sql);
				if(!$studentno){
					throw new Exception ( '学号为空不能合并！' );
				}
				$sql="update student set uid=".$p['student_uid']." and studentno=".$studentno." where studentid=".$value." and class_uid=".$p['class_uid'][$key];
				//echo $sql;exit;
		        $student=$this->_dbAdapter->query($sql);
				
				$isok=0;
				if(!$p['a_student_uid'][$key]){
					$result=$this->_dbAdapter->insert('student_class',array('uid_student'=>$p['a_student_uid'][$key],'uid_class'=>$p['class_uid'][$key]));
					if($result){
						$isok=1;
					}				
				}else{
					$sql='update student_class set uid_student='.$p['student_uid'].' where uid_student='.$p['a_student_uid'][$key].' and uid_class='.$p['class_uid'][$key];
					$result=$this->_dbAdapter->query($sql);
					if($result){
						$isok=1;
					}				
				}
				
			}
			if($isok)
				$this->feedback ( '更新成功', '注意', '/'.$this->controller .'/changeclass?studentname='.$p['cnname'], 'warning' );
			else	
				throw new Exception ( '更新失败 ！' );

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

/*以下几个是清脏数据*/
   public function clearparentAction() {
		try {
			$sql="SELECT * FROM `parents` where pid not in (SELECT pid from student where pid<>'')"; 
			$result=$this->_dbAdapter->fetchAll($sql);
			//print_r($result);exit;

		    $this->view->result=$result;	
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function opclearparentAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			
			if(!$p['htmlid'])
				throw new Exception ( '请选择 ！' );
			
			$isok=0;
			foreach($p['htmlid'] as $key=>$value){
				$res=$this->_dbAdapter->delete('parents','pid='.$value);
				if($res)
					$isok=1;
			}
			
			if($isok)
				$this->feedback ( '删除成功', '注意', '/'.$this->controller .'/clearparent', 'warning' );
			else	
				throw new Exception ( '删除失败 ！' );

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

   public function clearstudentAction() {
		try {
			$sql="SELECT uid,mobile,COUNT(mobile) as num,birthday,cnname,sex,groupid FROM `student` where 1 and mobile+0>0 GROUP BY mobile  HAVING num>1 ORDER BY COUNT(mobile) DESC"; 
			$result=$this->_dbAdapter->fetchAll($sql);
			//print_r($result);exit;

		    $this->view->result=$result;	
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
/*   public function editclearstudentAction() {
		try {
		    $mobile = $this->_request->getParam ( 'mobile' ); 		
			//print_r($mobile);exit;
			
			$sql="SELECT uid,mobile,birthday,cnname,sex,groupid FROM `student` where mobile='".$mobile."'"; 
			$result=$this->_dbAdapter->fetchAll($sql);
			//print_r($result);exit;

		    $this->view->result=$result;	
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
*/
	public function opclearstudentAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			
			if(!$p['htmlid'])
				throw new Exception ( '请选择 ！' );
			
			$isok=0;
			foreach($p['htmlid'] as $key=>$value){
				$res=$this->_dbAdapter->delete('parents','pid='.$value);
				if($res)
					$isok=1;
			}
			
			if($isok)
				$this->feedback ( '删除成功', '注意', '/'.$this->controller .'/clearstudent', 'warning' );
			else	
				throw new Exception ( '删除失败 ！' );

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

   public function studentnoclassAction() {
		try {
			$sql="SELECT * from student where uid not in (SELECT uid_student from student_class )"; 
			$result=$this->_dbAdapter->fetchAll($sql);
			//print_r($result);exit;

		    $this->view->result=$result;	
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opstudentnoclassAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			
			if(!$p['htmlid'])
				throw new Exception ( '请选择 ！' );
			
			$isok=0;
			foreach($p['htmlid'] as $key=>$value){
				$res=$this->_dbAdapter->delete('student','uid='.$value);
				if($res)
					$isok=1;
			}
			
			if($isok)
				$this->feedback ( '删除成功', '注意', '/'.$this->controller .'/studentnoclass', 'warning' );
			else	
				throw new Exception ( '删除失败 ！' );

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

   public function studentnocentralAction() {
		try {
			$sql='select groupid,groupname,groupname_en from groups where 1';
			$groups=$this->_dbAdapter->fetchAssoc($sql);
			foreach($groups as $key=>$value){
				$sql="SELECT * from `".$value['groupname_en']."`.student where uid>0 and uid not in (SELECT uid from central.student )"; 
				//$sql="SELECT * from `etonkids`.student where uid>0 and uid not in (SELECT uid from central.student )"; 
				//echo $sql;exit;
				$result[$key]['info']=$this->_dbAdapter->fetchAll($sql);
				$result[$key]['groupname']=$value['groupname'];
				$result[$key]['groupid']=$value['groupid'];
			}
			//print_r($result);exit;

		    $this->view->result=$result;	
		    $this->view->number=array_keys($groups);	
			//print_r(array_keys($groups));exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opstudentnocentralAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			
			if(!$p['htmlid'])
				throw new Exception ( '请选择 ！' );

			$sql='select groupid,groupname,groupname_en from groups where 1';
			$groups=$this->_dbAdapter->fetchAssoc($sql);
			
			$isok=0;
			foreach($p['htmlid'] as $key=>$value){
				$sql="update `".$groups[$p['groupid'][$value]]['groupname_en']."`.student set uid=0 where studentid='".$value."'"; 
				$res=$this->_dbAdapter->query($sql);
				if($res)
					$isok=1;
			}
			
			if($isok)
				$this->feedback ( '删除成功', '注意', '/'.$this->controller .'/studentnocentral', 'warning' );
			else	
				throw new Exception ( '删除失败 ！' );

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
   public function groupsAction() {
		try { 
         $sql="select * from  groups ";
	     $result = $this->_dbAdapter->fetchAll ($sql);
		 for($i = 0; $i<count($result); $i++)
		{
	      $str .= "<option value='".$result[$i]['groupid']."'>".$result[$i]['groupname']."</option>";
		}
		  
	    echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//获取所有的校园列表
	  public function schoollistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost ();  
		 
		  $sql="SELECT * FROM `groups` where groupid='".$p['id']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	 
			//连接运营库
			$dbArray=array();
			$dbArray['host']=$result['dbhost'];
			$dbArray['username']=$result['dbusername'];
			$dbArray['password']=$this->authcode($result['dbpassword'], 'DECODE', 'mactop') ;
			$dbArray['dbname']=$result['dbname'];
			
			$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
			Zend_Registry::set('dbAdapter', $dbAdapter);

			$dbAdapter=Zend_Registry::get('dbAdapter');
			$dbAdapter->query ( 'set names utf8');	
			 
		    $sql="select * from  school";   
			$schoollist= $dbAdapter->fetchAll ($sql);
			 
		 for($i = 0; $i<count($schoollist); $i++)
		{
		  $str .= "<option value='".$schoollist[$i]['schoolid'].'_'.$result['groupid']."'>".$schoollist[$i]['schoolname']."</option>";
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//获取所有的班级信息
	  public function classlistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost ();  
		  $array=explode('_',$p['id']);
		  
		  $sql="SELECT * FROM `groups` where groupid='".$array['1']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	 
			//连接运营库
			$dbArray=array();
			$dbArray['host']=$result['dbhost'];
			$dbArray['username']=$result['dbusername'];
			$dbArray['password']=$this->authcode($result['dbpassword'], 'DECODE', 'mactop') ;
			$dbArray['dbname']=$result['dbname'];
			
			$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
			Zend_Registry::set('dbAdapter', $dbAdapter);

			$dbAdapter=Zend_Registry::get('dbAdapter');
			$dbAdapter->query ( 'set names utf8');	
			 
			$sql="select * from  class  where isdel=0 and schoolid='".$array['0']."' ";   
			$classlist= $dbAdapter->fetchAll ($sql);
	  
			 
			for($i = 0; $i<count($classlist); $i++)
			{
			  $str .= "<option value='".$classlist[$i]['classid']."'>".$classlist[$i]['classname']."</option>";
			}
		   
	       echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
/*孩子转班*/	
	 public function transferAction() {
		try { 
	       $where='';	
		   $result=array();
		   $studentname =  trim($this->_request->getParam ( 'studentname' )); 
			
			if($studentname){
				$this->view->studentname = $studentname;
				
				$where .=' and  (cnname like "%'.$studentname.'%"  )';					
				$sql="select uid, pid,cnname,nikename,sex,birthday,realname,mobile,username,ischeck_mobile,isinstalled,groupname,groupid,schoolid,schoolname,classname,classid,uid_class from group_student_class_parents_v where ismainid=1 and inactive=0  ".$where;
				//echo $sql;
				$studentlist = $this->_dbAdapter->fetchAll ($sql);
				  
				//  print_r($studentlist); 
				if($studentlist){
					foreach($studentlist as $key => $value){
						if(!isset($result[$value['pid']])){
							$result[$value['pid']]['parent']['realname']=stripslashes($value['realname']);
							$result[$value['pid']]['parent']['pid']=$value['pid'];
							$result[$value['pid']]['parent']['mobile']=$value['mobile'];
							$result[$value['pid']]['parent']['username']=stripslashes($value['username']);
							$result[$value['pid']]['parent']['ischeck_mobile']=$value['ischeck_mobile'];
							$result[$value['pid']]['parent']['isinstalled']=$value['isinstalled'];
						}
						if(!isset($result[$value['pid']]['student'][$value['uid']])){
							$result[$value['pid']]['student'][$value['uid']]=array(
								'uid'=>$value['uid'],
								'cnname'=>stripslashes($value['cnname']),
								'nikename'=>stripslashes($value['nikename']),
								'sex'=>$value['sex'],
								'sexDisp'=>$value['sex']==1?'男':'女',
								'birthday'=>$value['birthday'],
								'age'=>$this->getAge($value['birthday']),
								
							);
						}

						$result[$value['pid']]['student'][$value['uid']]['class'][]=array(
						        'groupid'=>$value['groupid'],
								'schoolid'=>$value['schoolid'], 
								'groupname'=>$value['groupname'],
								'schoolname'=>$value['schoolname'],
								'classname'=>$value['classname'],
								'classid'=>$value['classid'], 
								'uid_class'=>$value['uid_class'],
						);
						 
					}
					
				}
			}		   
		   
		   $uid_class='';
		   foreach($studentlist as $key1=>$value1){
			   $classarr[]=$value1['classid'];
			   if(!$uid_class){
			   	   $uid_class.=$value1['uid_class'];
			   }else{
			   	   $uid_class.=','.$value1['uid_class'];
			   }

				 $sql="select * from student_class where classid ='".$value1['classid']."' and studentid='".$value1['uid']."'"; 
				// echo $sql; 
				 $nowstudent = $this->_dbAdapter->fetchRow ($sql); 
				 if($nowstudent){
				  $student_arr[]=$nowstudent['classid'];
				 }
		  } 
	   $this->view->student_arr=$student_arr;	
	   $this->view->result=$result;	
	   $this->view->uid_class=$uid_class;	
	//print_r($student_arr);echo '<br />';print_r($result);echo '<br />';print_r($uid_class);exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//查询本校园所有的班级
	
	 public function transferclassAction() {
		try { 
	    //获取传递的机构id 和 schoolid 和cnname 、classid 、uid_class
		 $studentname =  trim($this->_request->getParam ( 'cnname' )); 
		 $student_uid =  trim($this->_request->getParam ( 'student_uid' )); 
		 $uid_class =  trim($this->_request->getParam ( 'uid_class' )); 
	     $groupid =  trim($this->_request->getParam ( 'groupid' )); 
	     $schoolid =  trim($this->_request->getParam ( 'schoolid' )); 

		  
		 $this->view->studentname=$studentname;	
		 $this->view->groupid=$groupid;	
		 $this->view->studentid=$student_uid;	
		 $this->view->classid=$uid_class;
		 
		 //班级列表
    	 $sql="select * from class where schoolid='".$schoolid."' and isdel=0  ";
		 $classlist = $this->_dbAdapter->fetchAll ($sql);
		 $this->view->result=$classlist;	   
	     //print_r($studentname);print_r($student_uid);print_r($uid_class);exit; 
	    } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//选中的班级
	public function addtransferAction() {
		try { 
	    
	     if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost (); 
	 	  //print_r($p);exit;
		  //Array ( [uid] => Array ( [0] => 49375 ) [studentid] => 49479 [classid] => 49378 [groupid] => 2 ) 
		  $sql='select * from student_class where uid_student="'.$p['studentid'].'" and uid_class="'.$p['uid'][0].'"';
		  $res=$this->_dbAdapter->fetchAll($sql);
		  if($res){
		  	  $result2=$this->_dbAdapter->update('student_class',array('inactive'=>0),'uid_student="'.$p['studentid'].'" and uid_class="'.$p['uid'][0].'"');
		  }else{
			  $result2=$this->_dbAdapter->insert('student_class',array('uid_student'=>$p['studentid'],'uid_class'=>$p['uid'][0],'studentid'=>$p['studentid'],'classid'=>$p['uid'][0]));
		  }
			  $result1=$this->_dbAdapter->update('student_class',array('inactive'=>1),'uid_student="'.$p['studentid'].'" and uid_class="'.$p['classid'].'"');
/*		 //运营库中删除该学生的信息
		 $sql="select * from student where studentid='".$p['studentid']."' and classid='".$p['classid']."'";
		 $studentRow = $this->_dbAdapter->fetchRow ($sql);
		 //print_r($studentRow);
		 if($studentRow){
			  $res=$this->_dbAdapter->delete('student', 'studentid="'.$p['studentid'].'"'); 
		 }
		
		 //查询要转班级的class_uid
		 $sql="select * from class where classid='".$p['uid'][0]."'";
		 $classRow = $yunyingDb->fetchRow ($sql);
*/		 
/*		 //要转的班级里增加该学生的信息
		 do{
				$randcode=$this->randcode($p['groupid']) ;
				$sql="select * from student where bindcode='".$randcode."'";
				$result = $yunyingDb->fetchRow ( $sql ); 
				if($result){
					//需要重新生成 账号
					$y=1;
					}else{
					$y=0;   
						}
		 }while($y=0); 
		
		$isok=0;
		$data=array(
		     'uid'=>$studentRow['uid'],
		     'schoolid'=>$studentRow['schoolid'],
			 'classid'=>$classRow['classid'],
			 'class_uid'=>$classRow['uid'],
			 'cnname'=>$studentRow['cnname'],
			 'enname'=>$studentRow['enname'],
			 'adduserid'=>$this->_user->adminid,
			 'addtime'=>time(),
			 'bindcode'=>$randcode
		 ); 
		//查询这个学生在要转的班级是否存在studentid
		$sql="select studentid from student_class where uid_class='".$classRow['uid']."' and  uid_student = '".$studentRow['uid']."'";
		$studentid=$this->_dbAdapter->fetchOne($sql);
		
		
		
		if($studentid){
			//如果存在
		  	$data['studentid']=$studentid;
			
			$result = $yunyingDb->insert ('student',$data); 
		   // $lastid = $yunyingDb->lastInsertId ();
			if($result){
				$isok=1;
				}
			}else{
			//如果不存在	
			$result = $yunyingDb->insert ('student',$data); 
		    $lastid = $yunyingDb->lastInsertId ();  
			 //然后进行绑定 （student_class 增加即可绑定完成）
		$insert_res=$this->_dbAdapter->insert('student_class',array('uid_student'=>$studentRow['uid'],'uid_class'=>$classRow['uid'],'addtime'=>time(),'studentid'=>$lastid)); 
				if($insert_res){
					$isok=1;
					}
				}  
*/		 
		if($result1 && $result2){
			$this->feedback ( '转班成功', '成功', '/'.$this->controller .'/transfer/studentname/'.$p['studentname'], 'tip' );
		}else{
				throw new Exception ( '删除失败 ！' );
			} 
		  
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//生成随机号的
	 public function randcode($groupid,$len=5){
			
			$string = sprintf("%03d", $groupid); 
			//$num=substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 1); 
			
			 $str1 = "0123456789abcdefghijklmnopqrstuvwxyz";   //输出字符集    
			
			$strlen = strlen($str1)-1;  
			for($j=0 ; $j<$len ; $j++){  
					$s = substr($str1,rand(0,$strlen),1);  
					 $string .= $s;   
			}  
		  return $string; 
	   }
	
  /*离校*/
    public function leaveschoolAction() {
		try { 
	       
      	   $where='';	
		   $result=array();
		   $studentname =  trim($this->_request->getParam ( 'studentname' )); 
			
			if($studentname){
				$this->view->studentname = $studentname;
				
				$where .=' and  (cnname like "%'.$studentname.'%"  )';					
				$sql="select uid, pid,cnname,nikename,sex,birthday,realname,mobile,username,ischeck_mobile,isinstalled,groupname,groupid,schoolid,schoolname,classname,classid,uid_class from group_student_class_parents_v where ismainid=1 and inactive=0 ".$where;
				//echo $sql;
				$studentlist = $this->_dbAdapter->fetchAll ($sql);
				  
				//  print_r($studentlist); 
				if($studentlist){
					foreach($studentlist as $key => $value){
						if(!isset($result[$value['pid']])){
							$result[$value['pid']]['parent']['realname']=stripslashes($value['realname']);
							$result[$value['pid']]['parent']['pid']=$value['pid'];
							$result[$value['pid']]['parent']['mobile']=$value['mobile'];
							$result[$value['pid']]['parent']['username']=stripslashes($value['username']);
							$result[$value['pid']]['parent']['ischeck_mobile']=$value['ischeck_mobile'];
							$result[$value['pid']]['parent']['isinstalled']=$value['isinstalled'];
						}
						if(!isset($result[$value['pid']]['student'][$value['uid']])){
							$result[$value['pid']]['student'][$value['uid']]=array(
								'uid'=>$value['uid'],
								'cnname'=>stripslashes($value['cnname']),
								'nikename'=>stripslashes($value['nikename']),
								'sex'=>$value['sex'],
								'sexDisp'=>$value['sex']==1?'男':'女',
								'birthday'=>$value['birthday'],
								'age'=>$this->getAge($value['birthday']),
								
							);
						}

						$result[$value['pid']]['student'][$value['uid']]['class'][]=array(
						        'groupid'=>$value['groupid'],
								'schoolid'=>$value['schoolid'], 
								'groupname'=>$value['groupname'],
								'schoolname'=>$value['schoolname'],
								'classname'=>$value['classname'],
								'classid'=>$value['classid'], 
								'uid_class'=>$value['uid_class'],
						);
						 
					}
					
				}
			}		   
		   
		   $uid_class='';
		   foreach($studentlist as $key1=>$value1){
			   $classarr[]=$value1['classid'];
			   if(!$uid_class){
			   	   $uid_class.=$value1['uid_class'];
			   }else{
			   	   $uid_class.=','.$value1['uid_class'];
			   }
				 $sql="select * from student_class where classid ='".$value1['classid']."' and studentid='".$value1['uid']."'"; 
				// echo $sql; 
				 $nowstudent = $this->_dbAdapter->fetchRow ($sql); 
				 if($nowstudent){
				  $student_arr[]=$nowstudent['classid'];
				 }

		   } 
		//  print_r($student_arr); 
		  $this->view->student_arr=$student_arr;	
		   //print_r($num);exit;
		   $this->view->result=$result;	
		   $this->view->uid_class=$uid_class;	
 
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}





  public function leaveopAction() {
	try { 
     
         $studentname =  trim($this->_request->getParam ( 'cnname' )); 
		 $student_uid =  trim($this->_request->getParam ( 'student_uid' )); 
		 $uid_class =  trim($this->_request->getParam ( 'uid_class' )); 
	     $groupid =  trim($this->_request->getParam ( 'groupid' )); 
	     $schoolid =  trim($this->_request->getParam ( 'schoolid' )); 

		 $sql="select * from student_class where inactive=0 and classid ='".$uid_class."' and studentid='".$student_uid."'"; 
		 $student = $this->_dbAdapter->fetchAll ($sql); 
		 if($student){
		 	$res = $this->_dbAdapter->update ('student_class',array('inactive'=>1),"inactive=0 and classid ='".$uid_class."' and studentid='".$student_uid."'"); 
		 }else{
			  throw new Exception ( '删除失败 ！' );
		 }
		  
		 if($res){
			  $this->feedback ( '该学生已经离校', '成功', '/'.$this->controller .'/leaveschool', 'tip' );
		 }else{
			  throw new Exception ( '删除失败 ！' );
		 } 
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

